package com.adt;

import java.util.Iterator;

public class MySingleLinkedList<T> implements Iterable<T> {

	private Node<T> beginMarker;
	private Node<T> endMarker;

	private int modCount;

	private int theSize;

	private static class Node<T> {

		public Node(T data, Node<T> next) {
			this.data = data;
			this.next = next;
		}

		public T data;
		public Node<T> next;
	}

	public MySingleLinkedList() {
		clear();
	}

	private void clear() {
		beginMarker = new Node(null, null);
		endMarker = new Node(null, null);
		beginMarker.next = endMarker;
		modCount++;
		theSize = 0;
	}

	public int size() {
		return theSize;
	}

	public boolean add(T data) {
		add(size(), data);
		return true;
	}

	private boolean add(int idx, T nextData) {
		addNext(getNode(idx), nextData);
		return true;
	}

	private boolean addNext(Node<T> node, T nextData) {
		Node<T> nextNode = new Node<T>(nextData, null);
		Node<T> oldnext = node.next;
		node.next = nextNode;
		nextNode.next = oldnext;
		theSize++;
		modCount++;
		return true;
	}

	public Node<T> remove(int idx) {
		Node<T> p = getNode(idx);
		Node<T> temp = beginMarker;
		if (idx < 0 || idx > size()) {
			throw new IndexOutOfBoundsException();
		}

		for (int i = 0; i < size(); i++) {
			if (p == temp) {

			}
			temp = temp.next;

		}
		return null;
	}

	public Node<T> getNode(int idx) {
		Node<T> p = beginMarker;
		if (idx < 0 || idx > size()) {
			throw new IndexOutOfBoundsException();
		}

		for (int i = 0; i < size(); i++) {
			p = p.next;
			if (idx == i) {
				return p;
			}
		}
		return null;
	}

	@Override
	public Iterator<T> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

}
